import Vue from 'vue';
import CallDetail from './views/CallDetail.vue';
import CallDetailSobot from './views/CallDetailSobot.vue';
import CallDetailTianRun from './views/CallDetailTianRun.vue';
import http from '@src/util/http';

import { Loading } from 'element-ui';
import { getAsyncCalendarViewInitData } from '@src/api/InitDataApi'
import qs from 'qs';
import platform from '@src/platform';
/* service */
import CallCenterService from '@service/CallCenterService';
import { t } from '@src/locales'

const isCallCenterZhiChi = CallCenterService.getIsCallCenterZhiChi();

const isCallCenterTianRun = CallCenterService.getIsCallCenterTianRun();

let comp = CallDetail
if(isCallCenterZhiChi){
  comp = CallDetailSobot
}else if(isCallCenterTianRun){
  comp = CallDetailTianRun
}

Vue.prototype.$http = http;
Vue.prototype.$eventBus = new Vue();

const componentItem = (initData) => {
  return {
    provide: {
      initData: Object.freeze(initData)
    },
    render(h) {
      return h(comp);
    }
  }
}


Vue.component('async-component', function(resolve) {
  
  // 需要加loading 打开此注释
  // const instance = Loading.service({ fullscreen: true });
  
  let query = qs.parse(window.location.search.substr(1));

  const params = Object.assign({}, query, {})
  
  getAsyncCalendarViewInitData(params).then(result => {
    
    const initData = (result && result.data && result.data.initJson) || {}
    const title = (result && result.data && result.data.title) || ''
    const isSuccess = result.succ || result.success
    
    if (!isSuccess) {
      window.location.href = '/500'
      return 
    }
    
    try {
      window._init = JSON.stringify(initData)
    } catch (error) {
      console.error('initData 赋值失败')
    }
    
    // 需要加loading 打开此注释
    // instance.close();
    if (window.frameElement) {
      const currentTabId = window.frameElement.dataset.id;
      platform.setTabTitle({
        id: currentTabId,
        title
      })
    }
    resolve(componentItem(initData))
    
  }).catch(error => {
    console.error(error)
    window.location.href = '/500'
  })
  
});

let app = {
  render(h) {
    return <async-component />
  }
}

export default app;